﻿
using DataCollection.Core.Models;
using DataCollection.Infrastructure;
using Mapster;

namespace DataCollection.WinForm.Forms
{
    public partial class HistoryDataForm : DCBaseForm
    {
        #region 属性

        /// <summary>
        /// 试压记录编号
        /// </summary>
        public string SelectHistoryNumber = string.Empty;

        /// <summary>
        /// 当前页码
        /// </summary>
        private int PageNumber = 1;

        /// <summary>
        /// 每页数量
        /// </summary>
        private int PageSize = 30;

        /// <summary>
        /// 总数量
        /// </summary>
        private int TotalPages;

        #endregion

        #region 自定义方法
        /// <summary>
        /// 布局
        /// </summary>
        private void SetupForm()
        {
            ListTable.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("MainPressure","近端压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("MainMediumTemperature","近端环境温度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("MainBattery","近端电池电量",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("RemotePressure","远端压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("RemoteMediumTemperature","远端介质温度",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("RemoteBattery","远端电池电量",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Time","添加时间",AntdUI.ColumnAlign.Center)
            };
        }

        /// <summary>
        /// 列表
        /// </summary>
        public void LoadList()
        {
            Dictionary<string, object> where = new Dictionary<string, object>();
            where["history_number"] = SelectHistoryNumber;
            List<HistoryDataModel> list = HistoryDataModel.ListPage(where, PageNumber, PageSize, ref TotalPages);
            List<HistoryDataItem> itemList = new List<HistoryDataItem>();
            foreach (HistoryDataModel model in list)
            {
                HistoryDataItem item = new HistoryDataItem();
                model.Adapt(item);
                itemList.Add(item);
            }
            ListTable.DataSource = itemList;
            Pagination1.PageSize = PageSize;
            Pagination1.Total = TotalPages;
        }
        #endregion

        #region 系统事件
        public HistoryDataForm()
        {
            InitializeComponent();
            Header.Text = "历史点位";
            FullScreen = true;
        }

        private void HistoryDataForm_Load(object sender, EventArgs e)
        {
            SetupForm();
            DCSpin(this, "加载数据...", (action) =>
            {
                LoadList();
            }, null, () => { });
        }


        private void Pagination1_ValueChanged(object sender, AntdUI.PagePageEventArgs e)
        {
            PageNumber = e.Current;
            Task.Run(() =>
            {
                LoadList();
            });
        }

        #endregion

    }
}
